package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.ProcessInspectionItemRelationDto;
import com.be.beadmin.quality.dto.output.ProcessInspectionItemRelationEx;
import com.be.beadmin.quality.service.ProcessInspectionItemRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-05-28
**/
@RestController
public class ProcessInspectionItemRelationController {

    @Autowired
    private  ProcessInspectionItemRelationService processInspectionItemRelationService;


    @PostMapping(value = "/quality/getProcessInspectionItemRelationByPage")
    public ResultModel getProcessInspectionItemRelationByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(processInspectionItemRelationService.getProcessInspectionItemRelationByPage(pageEntity)) ;
    }

    @PostMapping("/quality/addProcessInspectionItemRelation")
    public ResultModel addProcessInspectionItemRelation(@RequestBody ProcessInspectionItemRelationDto processInspectionItemRelationDto){
        processInspectionItemRelationService.addProcessInspectionItemRelation(processInspectionItemRelationDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delProcessInspectionItemRelation")
    public ResultModel delProcessInspectionItemRelation(@RequestBody ProcessInspectionItemRelationDto processInspectionItemRelationDto){
        processInspectionItemRelationService.delProcessInspectionItemRelation(processInspectionItemRelationDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateProcessInspectionItemRelation")
    public ResultModel updateProcessInspectionItemRelation(@RequestBody ProcessInspectionItemRelationDto processInspectionItemRelationDto){
        processInspectionItemRelationService.updateProcessInspectionItemRelation(processInspectionItemRelationDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportProcessInspectionItemRelation")
    public void exportProcessInspectionItemRelation(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProcessInspectionItemRelationEx> processInspectionItemRelationPage = processInspectionItemRelationService.getProcessInspectionItemRelationByPage(pageEntity);
        List<ProcessInspectionItemRelationEx> processInspectionItemRelationExes = processInspectionItemRelationPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProcessInspectionItemRelation接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProcessInspectionItemRelationEx.class).sheet("sheet1").doWrite(processInspectionItemRelationExes);
    }
}